package com.org.cys.service.impl.information;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.org.cys.dao.base.IGeneralDao;
import com.org.cys.pojo.base.Page;
import com.org.cys.pojo.information.data.NewsEnum;
import com.org.cys.pojo.information.data.NewsEnum.NewsStatus;
import com.org.cys.service.information.NewsService;
import com.org.cys.util.IdWorker;
import com.org.cys.util.MapUtil;
import com.org.cys.util.PageData;
import com.org.cys.util.StringUtil;


/**
 *	t_information_news Service实现类
 *	@author caiyushen
 */
@Service("newsServiceImpl")
public class NewsServiceImpl implements NewsService {
  
  @Resource(name="generalDao")
  private IGeneralDao generalDao;
  
  public static String XMLMapper = "NewsMapper";
  
  /**
	* 装载News
	* @param PageData
	* @return
	* @throws Exception 
	* */
  @Override
  public PageData getEntityById(PageData pd) throws Exception {
	  return (PageData) generalDao.findForObject(XMLMapper+".findById", pd);
  }
  
  /**
	* 根据id装载News
	* @param PageData
	* @return
	* @throws Exception 
	* */
	@Override
	public PageData getEntityById(Long id) throws Exception {
		PageData pd = new PageData();
		pd.put("id",id);
		return (PageData) generalDao.findForObject(XMLMapper+".findById", pd);
	}
  
  /**
	* 保存News
	* @param PageData
	* @throws Exception
	* */
  @Override
  public void save(PageData pd) throws Exception{
  	  pd.put("id", IdWorker.getId());
  	  pd.put("newsStatus", NewsStatus.unpublish.getIndex());//默认未发布
  	  pd.put("createTime", new Date());
  	  pd.put("modifiedTime", new Date());
	  generalDao.save(XMLMapper+".save",pd);
  }
  
  /**
   * 批量保存News
   * @param PageData
   * @throws Exception
   * */
  public void saveBatch(List<PageData> pdList) throws Exception{
  	  for(PageData pd : pdList){
		  pd.put("id", IdWorker.getId());
		  pd.put("newsStatus", NewsStatus.unpublish.getIndex());//默认未发布
		  pd.put("createTime", new Date());
	  	  pd.put("modifiedTime", new Date());
	  }
	  generalDao.batchSave(XMLMapper+".saveBatch",pdList);
  }
  
  /**
	* 更新News
	* @param PageData
	* @throws Exception
	* */
  @Override
  public void update(PageData pd) throws Exception{
  	  pd.put("modifiedTime", new Date());
	  generalDao.update(XMLMapper+".update",pd);
  }
  
  /**
	* 删除News
	* @param PageData
	* @throws Exception
	* */
  @Override
  public void delete(PageData pd) throws Exception{
	  generalDao.delete(XMLMapper+".delete", pd);
  }
  
  /**
	* News列表
	* @param PageData
	* @throws Exception
	* */
  @Override
  public List<PageData> dataList(Page page) throws Exception{
	  return (List<PageData>) generalDao.findForList(XMLMapper+".dataListPage", page);
  }
  
  /**
	* 获取所有News数据
	* @param PageData
	* @throws Exception
	* */
  @Override
  public List<PageData> listAll(PageData pd) throws Exception{
	  return (List<PageData>) generalDao.findForList(XMLMapper+".listAll", pd);
  }
  
  /**
	* 批量删除News
	* @param ArrayDATA_IDS[]
	* @throws Exception
	* */
  @Override
  public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
	  generalDao.delete(XMLMapper+".deleteAll", ArrayDATA_IDS);
  }
  
  /**
   * 批量更改发布状态
   * @param ArrayDATA_IDS id数据集
   * @param newsStatus 发布状态 1:已发布 2:未发布
   * @throws Exception
   * */
  public void updateNewsStatus(String[] ArrayDATA_IDS,Integer newsStatus) throws Exception{
	  if(StringUtil.checkObj(newsStatus) && StringUtil.checkObj(ArrayDATA_IDS) && ArrayDATA_IDS.length>0){
		  PageData param = new PageData(); 
		  List<PageData> pdList = new ArrayList<>();	 
		  for(int i=0;i<ArrayDATA_IDS.length;i++){
			  PageData pd = new PageData();
			  pd.put("id", ArrayDATA_IDS[i]);
			  pdList.add(pd);
		  }
		  param.put("newsStatus", newsStatus);
		  param.put("pdList", pdList);
		  generalDao.update(XMLMapper+".updateNewsStatus",param);
	  }
 }
  
}